package com.sheepone.distributor.config;


import com.sheepone.distributor.util.NameGenerator;
import com.sheepone.util.algorithm.analysis.IntegerAnalysisUtil;
import com.sheepone.util.exception.ExceptionUtil;
import org.apache.commons.lang.StringUtils;

/**
 * @author Miss.杨
 * @description
 * @since 2023-12-07 22:04
 */
public class WorkerStationConfig {
    protected String station;
    protected int lineSize = 1024;
    protected int handlerNum = 1;
    protected int keepAliveTime = 60;

    protected double loadFactor = 1;

    public String getStation() {
        return station;
    }

    public void setStation(String stationName) {
        this.station = stationName;
    }

    public int getLineSize() {
        return lineSize;
    }

    public void setLineSize(int lineSize) {
        this.lineSize = lineSize;
    }

    public int getHandlerNum() {
        return handlerNum;
    }

    public void setHandlerNum(int handlerNum) {
        this.handlerNum = handlerNum;
    }

    public int getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(int keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public double getLoadFactor() {
        return loadFactor;
    }

    public void setLoadFactor(double loadFactor) {
        this.loadFactor = loadFactor;
    }

    public static WorkerStationConfigXmlBuilder builder() {
        return new WorkerStationConfigXmlBuilder();
    }

    public static class WorkerStationConfigXmlBuilder {
        private final WorkerStationConfig config = new WorkerStationConfig();

        public WorkerStationConfigXmlBuilder station(String stationName) {
            config.setStation(StringUtils.isBlank(stationName) ? NameGenerator.generateWorkerStationName() : stationName);
            return this;
        }

        public WorkerStationConfigXmlBuilder lineSize(String lineSize) {
            if (!StringUtils.isBlank(lineSize)) {
                ExceptionUtil.throwIllegalArgumentExceptionIf(!IntegerAnalysisUtil.isPositiveInteger((lineSize)), "lineSize must be a positive integer");
                int time = Integer.parseInt(lineSize);
                config.setLineSize(time);
            }
            return this;
        }

        public WorkerStationConfigXmlBuilder handlerNum(String handlerNum) {
            if (!StringUtils.isBlank(handlerNum)) {
                ExceptionUtil.throwIllegalArgumentExceptionIf(!IntegerAnalysisUtil.isPositiveInteger((handlerNum)), "handlerNum must be a positive integer");
                int time = Integer.parseInt(handlerNum);
                config.setHandlerNum(time);
            }
            return this;
        }

        public WorkerStationConfigXmlBuilder keepAliveTime(String keepAliveTime) {
            if (!StringUtils.isBlank(keepAliveTime)) {
                ExceptionUtil.throwIllegalArgumentExceptionIf(!IntegerAnalysisUtil.isPositiveInteger((keepAliveTime)), "keepAliveTime must be a positive integer");
                int time = Integer.parseInt(keepAliveTime);
                config.setKeepAliveTime(time);
            }
            return this;
        }

        public WorkerStationConfigXmlBuilder loadFactor(String loadFactor) {
            if (!StringUtils.isBlank(loadFactor)) {
                double time;
                try {
                    time = Double.parseDouble(loadFactor);
                } catch (Exception e) {
                    throw new IllegalArgumentException("loadFactor must be a double");
                }
                ExceptionUtil.throwIllegalArgumentExceptionIf(time < 0 || time > 1, "loadFactor must be a double between 0 and 1");
                config.setLoadFactor(time);
            }
            return this;
        }

        public WorkerStationConfig build() {
            return config;
        }
    }
}
